package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.pojo.EduCourse;
import com.atguigu.eduservice.pojo.EduTeacher;
import com.atguigu.eduservice.pojo.vo.CourseInfiVo;
import com.atguigu.eduservice.pojo.vo.CoursePublishVo;
import com.atguigu.eduservice.pojo.vo.CourseQuery;
import com.atguigu.eduservice.pojo.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-20
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;
    
    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfiVo courseInfiVo){
        //对该对象进行封装
        //加上返回值，返回你添加之后的课程的id，为了课程大纲去使用
       String id =  courseService.saveCourseInfo(courseInfiVo);

        return R.ok().data("courseId",id);
            
    }
    //根据课程id查询课程的基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfiVo courseInfiVo =  courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfiVo",courseInfiVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfiVo courseInfiVo){
        courseService.updateCourseInfo(courseInfiVo);
        return R.ok();
    }
    //根据课程id查询课程信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo =  courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }
    //课程最终发布
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(("Normal"));//已发布
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表接口 TODO


    //查询所有课程信息
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);

    }
    //分页查询课程信息
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable Long current,@PathVariable Long limit){
        //创建page对象
        Page<EduCourse> coursePage = new Page<>(current,limit);
        //调用分页服务
        courseService.page(coursePage,null);
        long total = coursePage.getTotal();//得到总记录数
        List<EduCourse> records = coursePage.getRecords();//数据的List集合
        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    //方法带条件查询的方法
//    @PostMapping("coursePage/{current}/{limit}")
//    public R pageCourseCondition(@PathVariable Long current,@PathVariable Long limit,
//                                 @PathVariable(required = false) CourseQuery courseQuery)

    //带条件的方法查询
    //方法叫条件查询带分页的方法
    @PostMapping("pageCourse/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){

        //创建一个page对象
        Page<EduCourse> page = new Page<>(current,limit);
        //

        //调用方法实现条件查询分页功能
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //排序条件
        wrapper.orderByDesc("gmt_create");

        //动态sql
        //判断条件值是否为空，如果不为空拼接条件值
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();

        if (!StringUtils.isEmpty(title)){
            //构建条件
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            //构建条件
            wrapper.eq("status",status);
        }


        //wrapper
        //调用方法实现条件查询分类
        courseService.page(page,wrapper);
        long total = page.getTotal();//总的记录数
        List<EduCourse> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }
    //删除课程
    @DeleteMapping("{courseId}")
    public R deletCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

